<?php

namespace Controller\Ajax;

class Article extends Base {

	public function save() {
		$this->mustLogin();
		$categoryId = $this->postInt('categoryId');
		$articleId = $this->postInt('articleId');
		$title = $this->postParam('title', true);
		$content = $this->postParam('content');
		$userId = $this->currentUserInfo['UserId'];
		try {
			$articleId = \Business\Article::instance()->saveArticle($articleId, $userId, $categoryId, $title, $content);
			$this->response($articleId);
		} catch (\Lib\Exception\Business $e) {
			$this->responseError($e->getMessage(), $e->getCode());
		}
	}

    public function del() {
        $this->mustLogin();
        $articleId = $this->postInt('articleId');
        $userId = $this->currentUserInfo['UserId'];
        \Business\Article::instance()->del($articleId, $userId);
        $this->response(true);
    }

	public function addComment() {
		$this->mustLogin();
		$userId = $this->currentUserInfo['UserId'];
		$articleId = $this->postInt('articleId');
		$content = $this->postParam('content');
		try {
			\Business\Article::instance()->addComment($userId, $articleId, $content);
			$this->response(true);
		} catch (\Lib\Exception\Business $e) {
			$this->responseError($e->getMessage(), $e->getCode());
		}
	}
}